let pc = require("./cp");
let xlsx = require("node-xlsx");
let path = require("path");
let dayjs = require("dayjs");
//加载文件模块
let fs = require("fs");
function findBall(redQz, blueQz) {
  let redBall = []; //红球结果
  let blueBall = []; //篮球结果
  let sumArr = [0]; //红球权重和数组
  let bsumArr = [0]; //篮球权重和数组

  // 计算红球权重和数组
  for (let i = 0; i < redQz.length; i++) {
    sumArr[i + 1] = sumArr[i] + redQz[i];
  }
  // console.log('权重数组', sumArr)
  // 计算蓝球权重和数组
  for (let j = 0; j < blueQz.length; j++) {
    bsumArr[j + 1] = bsumArr[j] + blueQz[j];
  }
  // for (let c = 1; c <= count; c++) {
  // 选红球
  while (redBall.length < 5) {
    //随机数 1 ~ 权重和
    let rnum = Math.floor(Math.random() * sumArr[sumArr.length - 1] + 1);
    let newBall = efFind(sumArr, rnum);
    if (!redBall.includes(newBall)) {
      redBall.push(newBall);
    }
  }
  //选篮球
  while (blueBall.length < 2) {
    //随机数 1 ~ 权重和
    let rnum = Math.floor(Math.random() * bsumArr[bsumArr.length - 1] + 1);
    let newBall = efFind(bsumArr, rnum);
    if (!blueBall.includes(newBall)) {
      blueBall.push(newBall);
    }
  }
  //二分边界查找
  function efFind(arr, target) {
    let left = 0;
    let right = arr.length;
    while (left < right) {
      let mid = left + Math.floor((right - left) / 2);
      let m = arr[mid];
      if (m == target) {
        right = mid;
      } else if (m > target) {
        right = mid;
      } else if (m < target) {
        left = mid + 1;
      }
    }
    return left;
  }
  redBall.sort((a, b) => a - b);
  blueBall.sort((a, b) => a - b);
  return [...redBall, ...blueBall];
}

async function start() {
  let res = await pc();
  console.log(res, "-------爬虫结果");
  let arr = [];
  //机选多少次  n
  for (let i = 1; i <= 10; i++) {
    arr.push(findBall(res.wzjred, res.wzjblue));
  }
  // let ball =
  // console.log('双手球结果', arr)
  exportExcel(arr, res.time);
  saveZj([...res.red, ...res.blue], res.time);
}
start();

function exportExcel(data, time) {
  //定义列宽，使用默认列宽，可以忽略
  // let sheetOptions = { "!cols": [{ wch: 10 }, { wch: 50 }, { wch: 20 }] };
  //生成buffer
  let date = dayjs(new Date()).format("YYYY-MM-DD");
  let dir = path.join(__dirname, "res/" + date);
  if (!fs.existsSync(dir)) {
    fs.mkdirSync(dir);
  }
  data.unshift([
    "红球1",
    "红球2",
    "红球3",
    "红球4",
    "红球5",
    "蓝球1",
    "蓝球2",
    time - 0 + 1,
  ]);

  let buffer = xlsx.build([{ name: "结果", data: data }]);
  //导出
  let filename = "大乐透" + Math.random().toString(16).slice(2, 6);
  fs.writeFileSync(
    path.join(__dirname, `/res/${date}/${filename}.xlsx`),
    buffer,
    { flag: "w" }
  );
}

function saveZj(res, time) {
  res.unshift(time);
  let data = res;
  let filepath = path.join(__dirname, "/zj/中奖号码.xlsx");
  if (fs.existsSync(filepath)) {
    let history = xlsx.parse(filepath);
    //输出一下
    let arr = history[0].data;
    let newRes = arr[arr.length - 1];
    let oldTime = newRes[0];
    if (oldTime == time) return;
    arr.push(data);
    data = arr;
  } else {
    let res = [data];
    res.unshift([
      "时间",
      "红球1",
      "红球2",
      "红球3",
      "红球4",
      "红球5",
      "蓝球1",
      "蓝球2",
    ]);
    data = res;
  }

  let buffer = xlsx.build([{ name: "中奖结果", data: data }]);
  //导出
  fs.writeFileSync(filepath, buffer, { flag: "w" });
  // let time=dayjs(Date.now()).format('yyyy-mm-d
}
